package io.lkt.modules.sys.controller;

import io.lkt.common.util.Query;
import io.lkt.common.utils.DateUtilPlus;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.modules.sys.entity.KeywordDetailLogEntity;
import io.lkt.modules.sys.entity.KeywordSumLogEntity;
import io.lkt.modules.sys.entity.KeywordSummaryEntity;
import io.lkt.modules.sys.entity.PushHeadLogEntity;
import io.lkt.modules.sys.service.KeywordDetailLogService;
import io.lkt.modules.sys.service.PushHeadLogService;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/sys/keywordlog")
public class KeywordLogController extends AbstractController{
    @Autowired
    private KeywordDetailLogService keywordDetailLogService;

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = keywordDetailLogService.queryPage(params);
        return R.ok().put("page", page);
    }

    @RequestMapping("/queryList")
    public R queryList(@RequestParam Map<String, Object> params) throws ParseException {
        if (params.get("gzhCode") == null || params.get("gzhCode").toString().trim().equals("")) {
            return R.ok();
        }

        if (params.get("startDate") != null && !params.get("startDate").toString().trim().equals("")) {

            String startDate = params.get("startDate").toString();
            params.put("startDate", startDate); //今天开始时间
            params.put("endDate", startDate + " 23:59:59"); //今天结束时间

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(startDate);

            String yesterday = DateUtilPlus.formatDate(DateUtilPlus.getYesterday(date));
            params.put("startYesterDay", yesterday); //昨天开始时间
            params.put("endYesterDay", yesterday + " 23:59:59"); //昨天结束时间

            String lastWeekToDay = DateUtilPlus.formatDate(DateUtilPlus.getToDayOfLastWeek(date));
            params.put("startLastWeekToDay", lastWeekToDay); //上周同期开始时间
            params.put("endLastWeekToDay", lastWeekToDay + " 23:59:59"); //上周同期结束时间

            String lastWeekFirstDay = DateUtilPlus.formatDate(DateUtilPlus.getFirstDayOfLastWeek(date));
            params.put("startLastWeekFirstDay", lastWeekFirstDay); //上周开始时间
            String lastWeekLastDay = DateUtilPlus.formatDate(DateUtilPlus.getLastDayOfLastWeek(date));
            params.put("endLastWeekLastDay", lastWeekLastDay + " 23:59:59"); //上周结束时间

            String lastMonthFirstDay = DateUtilPlus.formatDate(DateUtilPlus.getFirstDayOfLastMonth(date));
            params.put("startLastMonthFirstDay", lastMonthFirstDay); //上月开始时间
            String lastMonthLastDay = DateUtilPlus.formatDate(DateUtilPlus.getLastDayOfLastMonth(date));
            params.put("endLastMonthLastDay", lastMonthLastDay + " 23:59:59"); //上月结束时间

        }

        Query query = new Query(params);
        List<KeywordSumLogEntity> list=keywordDetailLogService.queryList(query);
        int total = keywordDetailLogService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
        return R.ok().put("page",pageUtil);
    }

    @RequestMapping("/summary")
    public R summary(@RequestParam Map<String, Object> params){
        if(params.get("gzhCode") == null || params.get("gzhCode").toString().trim().equals("")) {
            return R.ok();
        }

        if(params.get("startDate")!=null && !params.get("startDate").toString().trim().equals("")) {
            params.put("startDate", params.get("startDate"));
        }
        if(params.get("endDate")!=null && !params.get("endDate").toString().trim().equals("")) {
            params.put("endDate", params.get("endDate")+" 23:59:59");
        }

        KeywordSummaryEntity summary = keywordDetailLogService.getKeywordSummay(params);
        return R.ok().put("summary",summary);
    }


    @RequestMapping("/queryDetList")
    public R queryDetList(@RequestParam Map<String, Object> params){
        if(params.get("gzhCode") == null || params.get("gzhCode").toString().trim().equals("")) {
            return R.ok();
        }

        if(params.get("startDate")!=null && !params.get("startDate").toString().trim().equals("")) {
            params.put("startDate", params.get("startDate"));
        }
        if(params.get("endDate")!=null && !params.get("endDate").toString().trim().equals("")) {
            params.put("endDate", params.get("endDate")+" 23:59:59");
        }

        Query query = new Query(params);
        List<KeywordDetailLogEntity> list=keywordDetailLogService.queryDetList(query);
        int total = keywordDetailLogService.queryDetTotal(query);
        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
        return R.ok().put("page",pageUtil);
    }

    @RequestMapping("/userKeyword")
    public R userKeyword(@RequestParam Map<String, Object> params) {
        if (params.get("userId") == null) {
            return R.error("用户不存在");
        }

        Query query = new Query(params);
        List<KeywordDetailLogEntity> list = keywordDetailLogService.getUserKeyword(query);
        int total = keywordDetailLogService.getUserKeywordTotal(query).intValue();

        PageUtils page = new PageUtils(list, total, query.getLimit(), query.getPage());
        return R.ok().put("page", page);
    }

    @RequestMapping("/getUserKeywordCount/{userId}")
    public R getUserKeywordCount(@PathVariable("userId") Integer userId) {
        if (userId == null || userId.intValue() <= 0) {
            return R.error("用户不存在");
        }
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        int userKeywordCount = keywordDetailLogService.getUserKeywordTotal(params).intValue();
        return R.ok().put("userKeywordCount", userKeywordCount);
    }
}
